#ifndef DB3C94FF_E2BF_4738_9E55_3C81AF5A424F
#define DB3C94FF_E2BF_4738_9E55_3C81AF5A424F
#include "protocol.hpp"
#include<iostream>
using namespace std;

//上层业务
class Calculator
{
public:
    Response CalculatorHelper(Request req)
    {
        Response res(0, 0);
        res.result_ = res.code_ = 0;

        switch (req.op_)
        {
        case '+':
            res.result_ = req.x_ + req.y_;
            break;
        case '-':
            res.result_ = req.x_ - req.y_;
            break;
        case '*':
            res.result_ = req.x_ * req.y_;
            break;
        case '/':
        {
            if (req.y_ == 0)
            {
                res.code_ = 1;
                res.result_ = 0;
            }
            else
            {
                res.result_ = req.x_ / req.y_;
                res.code_ = 0;
            }
            break;
        }
        case '%':
        {
            if (req.y_ == 0)
            {
                res.code_ = 1;
                res.result_ = 0;
            }
            else
            {
                res.result_ = req.x_ % req.y_;
                res.code_ = 0;
            }
            break;
        }
        default:
        {
            res.code_ = 1;
            res.result_ = 0;
            break;
        }
        }
        return res;
    }

    string Handler(string &package) // 接受一个报文
    {
        string content;
        bool r = Decode(package, &content); // 将报文解包，如果这个时候报文， 第一个数字是\n说明后面一定是一个 然后把数据放到新的字符串中
        if (!r)
            return "";
        //
        Request req;              // 重新创建一个变量
        req.DeSerialize(content); // 然后将变量反序列化
        Response res = CalculatorHelper(req);

        content.clear();
        res.Serialize(&content);
        content = Encode(content);  
        cout << content << endl;
        return content;
    }
};

#endif /* DB3C94FF_E2BF_4738_9E55_3C81AF5A424F */
